<?php
namespace App\Models;
use Validator;

class Admins extends BaseModel{

    protected $table = 'admins';
    const SESSION_ADMIN_USER   = 'admins_user';


    public function __construct()
    {
        parent::__construct();
    }

    /**
     * @param $where
     * 获取管理员信息
     */
    public function getOne($where, $fileds='*'){
            return $this->_getOne($where, $fileds);
    }

    /**
     * 按条件查询全部数据,根据配置显示条数显示
     */
    public function getList(array $where=[], $fields = '*', $order = '', $pageSize = '')
    {
        if ($pageSize) {
            return $this->multiSelect($fields)->multiWhere($where)->multiOrder($order)->paginate($pageSize);
        } else {
            return $this->multiSelect($fields)->multiWhere($where)->multiOrder($order)->get();
        }
    }

    /**
     *插入数据
     */
    public function add($data)
    {
        return $this->_add($data);
    }


    /***
     * @param array $id
     * @param array $data
     * 更新数据
     */
    public function up($where,$data){
        return $this->_updata($where,$data);
    }

    /**
     * @param $id
     * 删除数据
     */
    public function del($id){
        return $this->_del(['id'=>$id]);
    }


    public function getAllAdmins()
    {
        return $this->getList();
    }

    public function getAdminList($keyword='', $pageSize=10)
    {
        $where = [];
        if ($keyword) {
            $index = 'username';
            if (is_numeric($keyword)) {
                $index = 'id';
            }
            $where[$index] = $keyword;
        }
        return $this->getList($where,'*',['id'=>'asc'],$pageSize);
    }


    public function getLoginAdmin($params)
    {
        $where['username'] = $params['username'];
        $where['password'] = md5($params['password']);
        $fields = 'id,nickname,gid,is_super_admin,state';
        return $this->getOne($where, $fields);
    }


    public function updateAdminLoginInfoById($id)
    {
        $where['id'] = $id;
        $data['last_login_time'] = time();
        $data['last_login_ip'] = request()->ip();
        return $this->up($where, $data);
    }

    public function getAdminById($id)
    {
        return $this->getOne(['id'=>$id]);
    }

    public function addAdmin($request)
    {
        $inputs['username'] = $request['username'];
        $inputs['password'] = $request['password'];
        $inputs['repassword'] = $request['repassword'];
        $inputs['nickname'] = $request['nickname'];
        $inputs = trim_fileds($inputs);
        $res['status'] = 1;
        $res['msg'] = '添加成功';
        $checkRes = $this->checkAddFields($inputs);
        if ($checkRes) {
            $res['status'] = 0;
            $res['msg'] = $checkRes;
            return $res;
        }
        if (!$this->add($this->combineAddFileds($inputs))) {
            $res['status'] = 0;
            $res['msg'] = '添加失败，请稍后再试！';
        }
        return $res;
    }

    public function combineAddFileds($inputs)
    {
        $data['username'] = $inputs['username'];
        $data['password'] = md5($inputs['password']);
        $data['nickname'] = $inputs['nickname'];
        $data['register_ip'] = request()->ip();
        $data['created_at'] =  date('Y-m-d H:i:s');
        return $data;
    }

    public function getAdminInfo($where)
    {
        return $this->getOne($where);
    }

    public function checkAddFields($inputs)
    {
        $message = '';
        $messages = [
            'username.required' => '用户名必须填写',
            'username.unique' => '该用户名已被使用',
            'username.max' => '用户名长度不能超过30个字符',
            'username.min' => '用户名长度不能小于6个字符',
            'nickname.required' => '昵称必须填写',
            'nickname.max' => '姓名不能大于10个字',
            'nickname.min' => '姓名不能少于1个字',
            'password.required' => '密码必须填写',
            'password.max' => '密码长度不能超过18位',
            'password.min' => '密码长度不能小于6位',
            'repassword.required' => '确认密码必须填写',
            'repassword.max' => '确认密码长度不能超过18位',
            'repassword.min' => '确认密码长度不能小于6位',
            'same' => '两次密码不一致',
        ];

        $validator = Validator::make(
            $inputs,
            [
                'username' => 'required|unique:admins|max:30|min:3',
                'nickname' => 'required|max:18|min:1',
                'password' => 'required|max:18|min:6',
                'repassword' => 'required|max:18|same:password|min:6',
            ],
            $messages
        );
        if ($validator->fails()) {
            $messages = $validator->errors()->all();
            //取第一个错误信息
            $message = $messages[0];
        }
        return $message;
    }

    public function checkEditFields($inputs)
    {
        $message = '';
        $messages = [
            'uid.required' => '修改失败，请重新再试',
            'nickname.required' => '昵称必须填写',
            'nickname.max' => '姓名不能大于10个字',
            'nickname.min' => '姓名不能少于1个字',
            'password.required' => '密码必须填写',
            'password.max' => '密码长度不能超过18位',
            'password.min' => '密码长度不能小于6位',
            'repassword.required' => '确认密码必须填写',
            'repassword.max' => '确认密码长度不能超过18位',
            'repassword.min' => '确认密码长度不能小于6位',
            'same' => '两次密码不一致',
        ];

        $validator = Validator::make(
            $inputs,
            [
                'uid' => 'required',
                'nickname' => 'required|max:18|min:1',
                'password' => 'required|max:18|min:6',
                'repassword' => 'required|max:18|same:password|min:6',
            ],
            $messages
        );
        if ($validator->fails()) {
            $messages = $validator->errors()->all();
            //取第一个错误信息
            $message = $messages[0];
        }
        return $message;
    }

    public function checkEditPwdFields($inputs)
    {
        $message = '';
        $messages = [
            'uid.required' => '修改失败，请重新再试',
            'nickname.required' => '昵称必须填写',
            'nickname.max' => '姓名不能大于10个字',
            'nickname.min' => '姓名不能少于1个字',
            'oldpassword.required' => '原始密码必须填写',
            'oldpassword.max' => '原始密码长度不能超过15位',
            'oldpassword.min' => '原始密码长度不能小于6位',
            'password.required' => '密码必须填写',
            'password.max' => '密码长度不能超过18位',
            'password.min' => '密码长度不能小于6位',
            'repassword.required' => '确认密码必须填写',
            'repassword.max' => '确认密码长度不能超过18位',
            'repassword.min' => '确认密码长度不能小于6位',
            'same' => '两次密码不一致',
        ];

        $validator = Validator::make(
            $inputs,
            [
                'uid' => 'required',
                'oldpassword' => 'required|max:15|min:6',
                'nickname' => 'required|max:18|min:1',
                'password' => 'required|max:18|min:6',
                'repassword' => 'required|max:18|same:password|min:6',
            ],
            $messages
        );
        if ($validator->fails()) {
            $messages = $validator->errors()->all();
            //取第一个错误信息
            $message = $messages[0];
        }
        return $message;
    }

    public function changeState($id, $state)
    {
        $where['id'] = $id;
        $data['state'] = $state;
        return $this->up($where, $data);
    }


    public function addCard($admin,$member,$num)
    {
        $this->start();
        try{
            $cards = $admin['cards'];
            $data['cards'] = $admin['cards']-$num;
            $admin['cards'] = $admin['cards']-$num;
            $where['id'] = $admin['id'];
            $this->up($where,$data);
            $adminCardLog = new AdminCardLog();
            $alogid = $adminCardLog->addLog($admin['id'],$cards,$member['id'],$member['cardnum'],$num);
            $this->commit();
            session(['admin'=>$admin]);
            return $alogid;
        } catch (\Exception $e) {
            $this->rollBack();
            return false;
        }
    }

}